version 16
* pause on

*******************   COMPUTING EQUILIBRIUM OUTCOMES UNDER PROCnorp

* use PROCmodel_data.dta, replace
* drop if vot_right==1
* by issue_id, sort: egen poptot = total(pop)

*  Compute most reformist country position
gsort issue_id cntry_pos
by issue_id: egen pivot_top=max(cntry_pos)

*  Compute pivot unanimity
gsort issue_id -cntry_pos
gen pivot_u=cntry_pos if issue_id!=issue_id[_n+1]

replace pivot_top=. if pivot_u==. /* to keep pivot_pop pivot_u on the same row */

* Compute pivot qmv
gen cumvote=votes_cou if cou_rule==2
local j = 1
while `j' <= 759 {
replace cumvote=cumvote+cumvote[_n-1] in `j' if issue_id==issue_id[_n-1]
local j = `j' + 1
}
generate pivot_qmv=cntry_pos if cumvote>=62 &   act==2 & cou_rule==2
replace pivot_qmv=cntry_pos 	if cumvote>=232 & (act==1 | act==3) & cou_rule==2
replace pivot_qmv=cntry_pos if cumvote>=255 & (act==4 | act==9  | act==11) & cou_rule==2
replace pivot_qmv=cntry_pos if cumvote>=157 & (act==8 | act==10 | act==12 | act==13) & cou_rule==2
replace pivot_qmv=pivot_qmv[_n-1] if pivot_qmv[_n-1]!=. & issue_id==issue_id[_n-1] & cou_rule==2 /* to ensure that the pivot value is the same in the column */

* Compute pivot in number of member states (14 & 9)
gen cumcount=1 if cou_rule==2
local j = 1
while `j' <= 759 {
replace cumcount=cumcount+cumcount[_n-1] in `j' if issue_id==issue_id[_n-1]
local j = `j' + 1
}
gen pivot_count14=cntry_pos if cumcount>=14 & cou_rule==2
replace pivot_count14=pivot_count14[_n-1] if pivot_count14[_n-1]!=. & issue_id==issue_id[_n-1] & cou_rule==2  /* to ensure that the pivot value is the same in the column */
gen pivot_count9=cntry_pos if cumcount>=9 & cou_rule==2
replace pivot_count9=pivot_count9[_n-1] if pivot_count9[_n-1]!=. & issue_id==issue_id[_n-1] & cou_rule==2  /* to ensure that the pivot value is the same in the column */

* Compute pivot in population
gen pop_s=pop/poptot
gen cumpop=pop_s if cou_rule==2
local j = 1
while `j' <= 759 {
replace cumpop=cumpop+cumpop[_n-1] in `j' if issue_id==issue_id[_n-1]
local j = `j' + 1
}
gen pivot_pop=cntry_pos if cumpop>=0.62 & cou_rule==2
replace pivot_pop=pivot_pop[_n-1] if pivot_pop[_n-1]!=. & issue_id==issue_id[_n-1] & cou_rule==2  /* to ensure that the pivot value is the same in the column */

* Compute pivot in qmv with pop rule and all member states 
egen pivot_min14=rowmin(pivot_qmv pivot_count14 pivot_pop) if pivot_qmv!=. & pivot_count14!=. & pivot_pop!=. & issue_id!=issue_id[_n+1]

* Compute pivot in qmv with pop rule and eurozone member states 
egen pivot_min9=rowmin(pivot_qmv pivot_count9 pivot_pop) if pivot_qmv!=. & pivot_count9!=. & pivot_pop!=. & issue_id!=issue_id[_n+1]

* pause

***** Compute PROC model prediction
*** Unanimity: the predicted outcome is the option, belonging to the support set S, that is most preferred by the Commission (act 5, 6, 7 & baseline for other acts)
* conservative Commission
gen PROCnorp=pivot_u   if pivot_u>=comm
* centrist Commission
replace PROCnorp=comm   if pivot_u<=comm & comm<=pivot_top
* reformist Commission
replace PROCnorp=pivot_top   if comm>=pivot_top

* pause

*** QMV Consultation (2005) with qmv: the predicted outcome is the option, belonging to the support set S, that is most preferred by the Commission (act 1)
* conservative Commission - as unanimity
* centrist Commission - as unanimity
* reformist Commission - as unanimity

* pause

*** QMV Consultation (2011) all countries qmv min14 : the predicted outcome is the option, belonging to the support set S, that is most preferred by the Commission (act 11)
* conservative Commission - as unanimity
* centrist Commission - as unanimity
* reformist Commission - as unanimity

* pause

*** Cooperation (acts 2 and 3) with qmv : the predicted outcome is the option, belonging to the support set S, that is most preferred by the Commission
* conservative Commission - as unanimity
* centrist Commission - as unanimity
* reformist Commission - as unanimity

* pause

*** OLP with qmv min14 (acts 4, 9): the predicted outcome is the option, belonging to the support set M, that is most preferred by the Commission
* conservative Commission
egen PROCnorp2=rowmin(ep_1 pivot_min14) if ((comm<=pivot_min14 & pivot_min14<=ep_1) | (comm<=ep_1 & ep_1<=pivot_min14)) & ep_rule==2 & issue_id!=issue_id[_n+1] & (act==4 | act==9) 
replace PROCnorp=PROCnorp2 if PROCnorp2!=.
drop PROCnorp2
* centrist Commission
replace PROCnorp=comm 				  if ((pivot_min14<=comm & comm<=ep_1) | (ep_1<=comm & comm<=pivot_min14)) 	  	  & ep_rule==2 & issue_id!=issue_id[_n+1] & (act==4 | act==9)  
* reformist Commission
egen PROCnorp2=rowmax(ep_1 pivot_min14) if ((pivot_min14<=ep_1 & ep_1<=comm) | (ep_1<=pivot_min14 & pivot_min14<=comm)) & ep_rule==2 & issue_id!=issue_id[_n+1] & (act==4 | act==9)  
replace PROCnorp=PROCnorp2 if PROCnorp2!=.
drop PROCnorp2

* pause

*** OLP with qmv min9 (acts 8, 10, 12, 13): the predicted outcome is the option, belonging to the support set M, that is most preferred by the Commission
* conservative Commission
egen PROCnorp2=rowmin(ep_1 pivot_min9) if ((comm<=pivot_min9 & pivot_min9<=ep_1) | (comm<=ep_1 & ep_1<=pivot_min9)) & ep_rule==2 & issue_id!=issue_id[_n+1] & (act==8 | act==10 | act>=12) 
replace PROCnorp=PROCnorp2 if PROCnorp2!=.
drop PROCnorp2
* centrist Commission
replace PROCnorp=comm 				 if ((pivot_min9<=comm & comm<=ep_1) | (ep_1<=comm & comm<=pivot_min9))		  & ep_rule==2 & issue_id!=issue_id[_n+1] & (act==8 | act==10 | act>=12)  
* reformist Commission
egen PROCnorp2=rowmax(ep_1 pivot_min9) if ((pivot_min9<=ep_1 & ep_1<=comm) | (ep_1<=pivot_min9 & pivot_min9<=comm)) & ep_rule==2 & issue_id!=issue_id[_n+1] & (act==8 | act==10 | act>=12)  
replace PROCnorp=PROCnorp2 if PROCnorp2!=.
drop PROCnorp2

order PROCnorp pivot_u pivot_qmv pivot_min14 pivot_min9 pivot_count14 pivot_count9 pivot_pop, after(outcome)

collapse PROCnorp outcome, by(issue_id)
label variable PROCnorp "procedural model solution, without reference point"

* Compute means of absolute differences between outcomes and model predictions
gen ad=abs(PROCnorp-outcome)
label variable ad "absolute difference bw PROCnorp outcome" 
egen mad = mean(ad)
label variable mad "mean absolute error per issue (PROCnorp)" 

